package com.yq.controller;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    static Socket socket;
    static ObjectOutputStream oos;
    static ObjectInputStream ois;

    //客户端
    public static void main(String[] args) {

        // 创建socket套接字，指定端口和ip
        try {
             socket = new Socket("127.0.0.1", 8000);
            //初始化对象流
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
        } catch (IOException exception) {
            exception.printStackTrace();
        }

        //跳到主菜单
        menu();

        try {
            //关闭流和socket
            ois.close();
            oos.close();
            socket.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    //1主菜单
    public static void menu() {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎进入企业级智能考勤系统，请登录");
        Integer e_id = EmployeePart.loginEmployee(oos, ois);
        //通过id得到role
        Integer role = EmployeePart.getRole(oos, ois, e_id);
        //不同role进入不同菜单
        if (role == 1) {
            System.out.println("管理员登录成功，进入主菜单");
            adminMenu(sc, e_id);
        } else if (role == 0) {
            System.out.println("普通用户登录成功，进入主菜单");
            userMenu(sc, e_id);
        }
    }

    //1.1管理员界面
    private static void adminMenu(Scanner sc, Integer e_id) {
        System.out.println("1、注册员工信息\t2、查询/统计统计员工考勤信息\t3、退出登录\t4、退出系统");
        int option = Integer.parseInt(sc.next());
        switch (option) {
            case 1:
                //注册（创建）新用户
                EmployeePart.registerEmployee(oos, ois);
                adminMenu(sc, e_id);
                break;
            case 2:
                //查询统计考勤信息
                EmployeePart.ClockWorkAll(oos, ois);
                adminMenu(sc, e_id);
                break;
            case 3:
                System.out.println("退出登录成功");
                menu();
                break;
            case 4:
                System.out.println("再见");
                break;
            default:
                break;
        }
    }

    //1.1.1按不同条件查询
    /*public static void adminSelect(Scanner sc){
        System.out.println("1、查询所有记录\t2、按员工编号查询\t3、按员工姓氏查询\t4、按年/月/日查询\t5、返回");
        System.out.println("请输入：");
        int option = Integer.parseInt(sc.next());
        switch (option){
            case 1:
                EmployeePart.ClockWorkAll(oos,ois);
                adminSelect(sc);
                break;
            case 2:
                EmployeePart.ClockWorkNo(oos,ois);
                adminSelect(sc);
                break;
            case 3:
                EmployeePart.ClockWorkName(oos,ois);
                adminSelect(sc);
                break;
            case 4:
                EmployeePart.ClockWorkDate(oos,ois);
                adminSelect(sc);
                break;
            case 5:
                break;
        }*/

    //1.2用户界面
    private static void userMenu(Scanner sc, Integer e_id) {
        //通过id获得工号
        String no = EmployeePart.getNo(oos, ois, e_id);
        System.out.println("1、上班打卡\t2、下班打卡\t3、查看个人考勤信息\t4、退出登录\t5、退出系统");
        int option = Integer.parseInt(sc.next());
        switch (option) {
            case 1:
                //通过工号打卡
                EmployeePart.inTimeClock(oos, ois, no);
                userMenu(sc, e_id);
                break;
            case 2:
                EmployeePart.offTimeClock(oos, ois, no);
                userMenu(sc, e_id);
                break;
            case 3:
                //通过工号查看考勤信息
                EmployeePart.ClockWork(oos, ois, no);
                userMenu(sc, e_id);
                break;
            case 4:
                System.out.println("退出登录成功");
                menu();
                break;
            case 5:
                System.out.println("再见");
                break;
            default:
                break;
        }
    }
}
